package com.ruoyi.web.controller.site;


import cn.hxzy.common.SiteAttributeType;
import cn.hxzy.common.SiteType;
import cn.hxzy.common.VoucherType;
import cn.hxzy.common.util.RandomId;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.hxzy.site.entity.TtSite;
import com.hxzy.site.service.TtSiteService;
import com.hxzy.system.service.TdBusinessAffiliationService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mingqian.Wu
 * @since 2023-06-24
 */
@RestController
@RequestMapping("/ttSite")
@DataSource(DataSourceType.SITE)
public class TtSiteController {




    @Autowired
    private TtSiteService ttSiteService;
//
//    @Autowired
//    private TdBusinessAffiliationService tdBusinessAffiliationService;
//

    @GetMapping("/getSiteList/{pageIndex}/{pageSize}")
    @Anonymous
    public AjaxResult getSiteList(@PathVariable Integer pageIndex, @PathVariable Integer pageSize) {
        if (pageIndex!=-1&&pageSize!=-1){
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<TtSite> list = ttSiteService.list();
        PageInfo pageInfo = new PageInfo(list);
        return AjaxResult.success(list).put("total", pageInfo.getTotal());
    }


    @PostMapping("searchSiteList/{pageIndex}/{pageSize}")
    public AjaxResult searchSiteList(@PathVariable Integer pageIndex, @PathVariable Integer pageSize, @RequestBody TtSite ttSite) {
        PageHelper.startPage(pageIndex, pageSize);
        List<TtSite> list = ttSiteService.searchSite(ttSite);
        PageInfo pageInfo = new PageInfo(list);
        return AjaxResult.success(list).put("total", pageInfo.getTotal());
    }


    @GetMapping("getSiteInfo/{id}/{status}")
    public AjaxResult getSiteInfo(@PathVariable String id,@PathVariable Integer status) {
        TtSite byId = ttSiteService.getById(id);
        if (status==1){
            byId.setAttribute(SiteAttributeType.getSiteAttributeTypValue(Integer.parseInt(byId.getAttribute())));
            byId.setSiteType(SiteType.getSiteTypeValue(Integer.parseInt(byId.getSiteType())));
            byId.setVoucherType(VoucherType.getVoucherTypeValue(Integer.parseInt(byId.getVoucherType())));
            byId.setSign(SiteType.getSiteTypeValue(Integer.parseInt(byId.getSign())));
        }

        return AjaxResult.success(byId);
    }


    @PutMapping("/transportConfiguration")
    public AjaxResult transportConfiguration(@RequestBody TtSite ttSite) {
        UpdateWrapper<TtSite> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", ttSite.getId());
        boolean update = ttSiteService.update(ttSite, updateWrapper);
        if (update) {
            return AjaxResult.success("配置成功");
        }
        return AjaxResult.error("配置失败");
    }

    @PostMapping("/addSite")
    public AjaxResult addSite(@RequestBody TtSite ttSite) {
        ttSite.setId(RandomId.getSiteId());
        ttSite.setCreateTime(new Date());
        boolean save = ttSiteService.save(ttSite);
        if (save) {
            return AjaxResult.success("新建成功");
        }
        return AjaxResult.error("新建失败");
    }

    @PutMapping("updateSite")
    public AjaxResult updateSite(@RequestBody TtSite ttSite){
        UpdateWrapper<TtSite>updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",ttSite.getId());
        boolean update = ttSiteService.update(ttSite, updateWrapper);
        if (update){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    @DeleteMapping("/deleteSite/{id}")
    public AjaxResult deleteSite(@PathVariable String id){
        boolean b = ttSiteService.removeById(id);
        if (b){
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }


}

